﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.Controls.Document
{
  public partial class TableCellControl : UserControlExtended
  {
    private Border _borderBottom;
    private Border _borderTop;
    private Border _borderLeft;
    private Border _borderRight;

    private double _marginTop;
    private double _marginRight;
    private double _marginBottom;
    private double _marginLeft;

    private int _columnSpan;
    private int _rowSpan;

    private Brush _background;

    public UIElementCollection CellContent
    {
      get { return ContentControl.Children; }
    }

    public int ColumnSpan
    {
      get { return _columnSpan; }
      set { _columnSpan = value; }
    }

    public int RowSpan
    {
      get { return _rowSpan; }
      set { _rowSpan = value; }
    }

    public new Brush Background
    {
      get { return _background; }
      set { _background = value; }
    }

    public Border BorderTop
    {
      get { return _borderTop; }
      set { _borderTop = value; }
    }

    public Border BorderLeft
    {
      get { return _borderLeft; }
      set { _borderLeft = value; }
    }

    public Border BorderRight
    {
      get { return _borderRight; }
      set { _borderRight = value; }
    }

    public Border BorderBottom
    {
      get { return _borderBottom; }
      set { _borderBottom = value; }
    }

    public double MarginTop
    {
      get { return _marginTop; }
      set { _marginTop = value; }
    }

    public double MarginRight
    {
      get { return _marginRight; }
      set { _marginRight = value; }
    }

    public double MarginBottom
    {
      get { return _marginBottom; }
      set { _marginBottom = value; }
    }

    public double MarginLeft
    {
      get { return _marginLeft; }
      set { _marginLeft = value; }
    }

    public TableCellControl()
    {
      InitializeComponent();

      _borderBottom = new Border();
      _borderTop = new Border();
      _borderLeft = new Border();
      _borderRight = new Border();
    }

    protected override Size CustomMeasureOverride(Size availableSize)
    {
      if (_rowSpan == 0)
        return new Size(Width, 0);

      if (!double.IsNaN(Height))
        return new Size(Width, Height);

      Size textBlockSize = new Size(Width - _borderLeft.Width - _borderRight.Width - _marginLeft - _marginRight, double.PositiveInfinity);
      ContentControl.CustomMeasure(textBlockSize);

      return new Size(Width, ContentControl.CustomDesiredSize.Height + _marginTop + _marginBottom + (_borderTop.Width / 2) + (_borderBottom.Width / 2));
    }

    protected override Size CustomArrangeOverride(Size finalSize)
    {
      if (_rowSpan == 0)
        return new Size(Width, 0);

      Point borderTopLeft = new Point(_borderLeft.Width, _borderTop.Width);
      Point textBlockTopLeft = new Point(borderTopLeft.X + _marginLeft, borderTopLeft.Y + _marginTop);

      ContentControl.SetLocation(textBlockTopLeft);
      ContentControl.CustomArrange(LayoutUtils.CreateRect(LayoutUtils.Zero, ContentControl.CustomDesiredSize));

      Rect borderRect = LayoutUtils.CreateRect(LayoutUtils.Zero, finalSize);// new Rect(0, 0, Width, Height);

      if (_background != null)
      {
        BackgroundCanvas.Background = _background;
        BackgroundCanvas.SetBounds(borderRect);
      }

      if (_borderBottom.IsVisible)
      {
        BorderBottomLine.Stroke = new SolidColorBrush(_borderBottom.Color);
        BorderBottomLine.StrokeThickness = _borderBottom.Width;
        BorderBottomLine.SetBounds(new Rect(borderRect.GetBottomLeft(), borderRect.GetBottomRight()));
      }
      if (_borderTop.IsVisible)
      {
        BorderTopLine.Stroke = new SolidColorBrush(_borderTop.Color);
        BorderTopLine.StrokeThickness = _borderTop.Width;
        BorderTopLine.SetBounds(new Rect(borderRect.GetTopLeft(), borderRect.GetTopRight()));
      }
      if (_borderLeft.IsVisible)
      {
        BorderLeftLine.Stroke = new SolidColorBrush(_borderLeft.Color);
        BorderLeftLine.StrokeThickness = _borderLeft.Width;
        BorderLeftLine.SetBounds(new Rect(borderRect.GetTopLeft(), borderRect.GetBottomLeft()));
      }
      if (_borderRight.IsVisible)
      {
        BorderRightLine.Stroke = new SolidColorBrush(_borderRight.Color);
        BorderRightLine.StrokeThickness = _borderRight.Width;
        BorderRightLine.SetBounds(new Rect(borderRect.GetTopRight(), borderRect.GetBottomRight()));
      }

      return finalSize;
    }
  }
}
